package oray

import (
	"fmt"
	"github.com/Jackarain/ddns/dnsutils"
	"io/ioutil"
	"log"
	"net/http"
	"tiger-ddns/src/config"
	"time"
)

const API_URL = "http://ddns.oray.com/ph/update"

func Update(config *config.Config, currentIP string) {
	// 发起更新请求
	client := &http.Client{Timeout: 10 * time.Second}
	req, _ := http.NewRequest("GET",
		fmt.Sprintf("%s?hostname=%s&myip=%s", API_URL, config.Oray.Domain, currentIP), nil)

	token := dnsutils.BasicAuth(config.Oray.Username, config.Oray.Password)
	req.Header.Add("Authorization", "Basic "+token)

	resp, err := client.Do(req)
	if err != nil {
		log.Printf("🚨 API请求失败: %v", err)
		return
	}
	defer resp.Body.Close()

	body, _ := ioutil.ReadAll(resp.Body)
	log.Printf("🎉 更新结果: %s", string(body))
}
